<?php

class XMLDataParser extends DataParser {

    public function parseData($data) {
        $group = $this->getOption('group');
        $name = $this->getOption('name');
        $groupname = $this->getOption('groupname');
        $xml = simplexml_load_string($data);
        $json = json_encode($xml);
        $items = json_decode($json, TRUE);
        $arr = $items['row'];
        $groups = array();
        $array = array();
        foreach ($arr as $i => $item) {
            if (isset($group) && !in_array($item[$group], $groups)) {
                $groups[] = $item[$group];
            }
            if (isset($groupname)) {
                if ($item[$group] == $groupname) {
                    $array[] = $item;
                }
            } else {
                $array[] = $item;
            }
        }
        $groupArrs = array();
        if (isset($group) && !isset($groupname)) {
            foreach ($groups as $item) {
                $groupArr[$name] = $item;
                $groupArrs[] = $groupArr;
            }
            $array = $groupArrs;
        }
        $this->setTotalItems(count($array));
        return $array;
    }
}
